package com.demo.controller;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import javax.imageio.ImageIO;

import com.demo.constant.ErrorInfo;
import com.demo.kit.StrKit;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.upload.UploadFile;

public class UploadController extends BaseController {

	public void upload(){
		String dirPath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		String targetPath = PathKit.getWebRootPath() + "\\..\\"
				+ PropKit.get("imgPath") + "\\" + dirPath;
		String fileTargetPath = PropKit.get("imgPath") + "\\" + dirPath; 
		
		File targetDir = new File(targetPath);
		if (!targetDir.exists()) {
			targetDir.mkdirs();
		}
		UploadFile tempFile = getFile("image", fileTargetPath);
		if (null == tempFile) {
			List<UploadFile> uploadFiles = getFiles(fileTargetPath);
			for (UploadFile uploadFile : uploadFiles) {
				uploadFile.getFile().delete();
			}
			renderDataJson(ErrorInfo.ERR_IMAGE_NOT_EMPTY);
			return;
		}
		String ext = tempFile.getFileName().substring(
				tempFile.getFileName().lastIndexOf("."));
		String uuid = StrKit.getRandomUUID();	// uuid = aaaaa
		String imgPath = uuid + ext;     		// aaaaa.jpg
		tempFile.getFile().renameTo(new File(targetPath + "\\"+ uuid + ext));	// 修改文件名称
		//数据库保存的路径  /serviceImg/yyyy-mm-dd/aaaaa.jpg
		String dbPath = "/" + PropKit.get("imgPath") + "/" + dirPath + "/" + uuid + ext; 
		File file = new File(targetPath + "\\"+ uuid + ext);
		try {
			InputStream inputStream = new FileInputStream(file.getPath());
			BufferedImage prevImage = ImageIO.read(inputStream);
			// 判断该文件是不是图片
			if (prevImage == null) {
				inputStream.close();
				System.out.println("不是图片");
				if (!tempFile.getFile().delete()) {
					renderDataJson(ErrorInfo.ERR_FAIL);
					return;
				}
				renderDataJson(ErrorInfo.ERR_FILE_IS_NOT_IMAGE);
				return;
			}
			// 是否需要裁剪图片
//			ImageUtil.resizeImage(prevImage, inputStream, imgPath, targetPath, 800, 200);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		renderDataJson(ErrorInfo.ERR_SUCCESS, dbPath);
	}
	
	/**
	 * 删除图片
	 */
	@SuppressWarnings("rawtypes")
	public void delete(){
		String webPath = PathKit.getWebRootPath(); // D:\apache-tomcat-7.0.73\webapps\project
		webPath = webPath.substring(0, webPath.lastIndexOf("\\"));	// D:\apache-tomcat-7.0.73\webapps
		Enumeration enu = getParaNames();
		while (enu.hasMoreElements()) {
			String fileName = (String) enu.nextElement();
			String filePath = StrKit.getImgPath(getPara(fileName), "/");
			String path = webPath +filePath;
			File file = new File(path);
			if (!file.exists()) {
				renderDataJson(ErrorInfo.ERR_DEL_IMAGE_FAIL);
				return;
			}
			file.delete();
		}
		renderDataJson(ErrorInfo.ERR_SUCCESS);
	}
}
